home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / src / resoluti.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  4.5 KB  |  235 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. //   - Falcon resolutions are not supported by this module, due to lack of info
  13. //   - TTHigh not tested.
  14. //   - Need to care for Falcon emulation of TTMedium (and TTLow?), if possible.
  15. //
  16.  
  17. #include "Resolution.h"
  18. #include "Cookie.h"
  19. #include <osbind.h>
  20. #include <falcon.h>
  21.  
  22. const int NUMRES=8;
  23.                                         /* STL STM STH ??? TTM  ???  TTH TTL */
  24. static short StdWidth[NUMRES]            = {320,640,640,640,640,1280,1280,320};
  25. static short StdHeight[NUMRES]            = {200,200,400,400,480, 960, 960,480};
  26. static short StdDepth[NUMRES]            = {  4,  2,  1,  1,  4,   1,   1,  8};
  27. static short StdTTOnly[NUMRES]            = {  0,  0,  0,  0,  1,   1,   1,  1};
  28.  
  29. const long NO_MODE=0x20000;
  30. const long FALC_MODE=0x10000;
  31.  
  32. // TERMINATION code:
  33.  
  34. // used to ensure MyRezRestorer is linked in if possibly necessary
  35. #define EnsureRestoration { volatile RezRestorer* x=&MyRezRestorer; }
  36.  
  37. class RezRestorer
  38. {
  39. public:
  40.     RezRestorer() { }
  41.     ~RezRestorer() {
  42.         Resolution Now;
  43.         if (Now!=InitialResolution) InitialResolution.Use();
  44.     }
  45. private:
  46.     Resolution InitialResolution;
  47. };
  48.  
  49. static RezRestorer MyRezRestorer;
  50.  
  51.  
  52. static
  53. long ModeFor(int w, int h, int d)
  54. {
  55.     for (int m=0; m<NUMRES; m++) {
  56.         if (w==StdWidth[m] && h==StdHeight[m] && d==StdDepth[m]) {
  57.             if (StdTTOnly[m]) {
  58.                 if (GetCookieNamed("_VDO") == 0x20000) {
  59.                     // It's a TT.
  60.                     return m;
  61.                 }
  62.             } else {
  63.                 return m;
  64.             }
  65.         }
  66.     }
  67.  
  68.     montypes mon=Montype();
  69.  
  70.     if (int(mon)==-32 || int(mon)==89) {
  71.         // Not a Falcon.
  72.         return NO_MODE;
  73.     }
  74.  
  75.     long mode=FALC_MODE|(Vsetmode(-1)&PAL); // PAL if already PAL.
  76.  
  77.     switch (w) {
  78.      case 640:
  79.         mode|=COL80;
  80.     break; case 320:
  81.         mode|=0;
  82.     break; case 384:
  83.         mode|=OVERSCAN;
  84.     break; case 768:
  85.         mode|=COL80|OVERSCAN;
  86.     break; default:
  87.         return NO_MODE;
  88.     }
  89.  
  90.     switch (h) {
  91.      case 200:
  92.         mode|=STMODES;
  93.     break; case 240:
  94.         mode|=0;
  95.     break; case 288:
  96.         mode|=OVERSCAN;
  97.     break; case 400:
  98.         mode|=STMODES;
  99.     break; case 480:
  100.         mode|=0;
  101.     break; case 576:
  102.         mode|=OVERSCAN;
  103.     break; default:
  104.         return NO_MODE;
  105.     }
  106.  
  107.     if (h<400 && (mon==VGAcolor || mon==STmono)     // Line doubled
  108.      || h>399 && (mon==STcolor || mon==TVcolor)) {  // or Interlaced
  109.         mode|=VERTFLAG;
  110.     }
  111.  
  112.     switch (d) {
  113.      case 1:
  114.         mode|=BPS1;
  115.     break; case 2:
  116.         mode|=BPS2;
  117.     break; case 4:
  118.         mode|=BPS4;
  119.     break; case 8:
  120.         mode|=BPS8;
  121.     break; case 16:
  122.         mode|=BPS16;
  123.     break; default:
  124.         return NO_MODE;
  125.     }
  126.  
  127.     return mode;
  128. }
  129.  
  130. static void SetMode(long mode)
  131. {
  132.     switch (mode) {
  133.      case 0: case 1:
  134.      case 2: case 7:
  135.      case 4: case 5:
  136.         Setscreen(-1,-1,mode,0);
  137.     break; case NO_MODE:
  138.         ; // ignore
  139.     break; 
  140.         if (mode&FALC_MODE) {
  141.             // Falcon...
  142.             Vsetmode(mode^FALC_MODE);
  143.         }
  144.     }
  145. }
  146.  
  147. Resolution::Resolution(short r)
  148. {
  149.     EnsureRestoration;
  150.  
  151.     switch (r) {
  152.      case 0: case 1: case 2: case 7: case 4: case 5:
  153.         w=StdWidth[r];
  154.         h=StdHeight[r];
  155.         d=StdDepth[r];
  156.         mode=ModeFor(w,h,d);
  157.     break; default:
  158.         mode=NO_MODE;
  159.     }
  160. }
  161.  
  162. Resolution::Resolution()
  163. // Current
  164. {
  165.     EnsureRestoration;
  166.  
  167.     int r=Getrez();
  168.  
  169.     switch (r) {
  170.      case 0: case 1: case 2: case 7: case 4: case 5:
  171.         w=StdWidth[r];
  172.         h=StdHeight[r];
  173.         d=StdDepth[r];
  174.         mode=ModeFor(w,h,d);
  175.     break;
  176.         // Falcon...  Mode stored in lower 16 bits of mode.  Bit 16 set.
  177.         mode=FALC_MODE|Vsetmode(-1);
  178.     break; default:
  179.         mode=NO_MODE;
  180.     }
  181. }
  182.  
  183. Resolution::Resolution(int width, int height, int depth) :
  184.     w(width),h(height),d(depth),
  185.     mode(ModeFor(w,h,d))
  186. {
  187.     EnsureRestoration;
  188. }
  189.  
  190.  
  191.  
  192. bool Resolution::Usable() const
  193. {
  194.     return mode!=NO_MODE;
  195. }
  196.  
  197. void Resolution::Use() const
  198. {
  199.     SetMode(mode);
  200. }
  201.  
  202.  
  203. unsigned int Resolution::NumberOfColours() const
  204. {
  205.     static unsigned int Lookup[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
  206.     if (d<=16) return Lookup[d];
  207.  
  208.     unsigned int r=65536*2;
  209.     unsigned int td=d-17;
  210.     while (td--) r=r*2;
  211.  
  212.     return r;
  213. }
  214.  
  215. long Resolution::Size() const
  216. {
  217.     if (mode&FALC_MODE) {
  218.         return VgetSize(mode^FALC_MODE);
  219.     } else {
  220.         return w*h*d/8;
  221.     }
  222. }
  223.  
  224. const Resolution STLow(320,200,4);
  225. const Resolution STMedium(640,200,2);
  226. const Resolution STHigh(640,400,1);
  227.  
  228. const Resolution TTLow(320,480,8);
  229. const Resolution TTMedium(640,480,4);
  230. const Resolution TTHigh(1280,960,1);
  231.  
  232. // const Resolution MaximumResolution;  How to implement?
  233.  
  234.  
  235.